/*
 * Copyright 2013-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package kn.pulldata.controller;


import kn.pulldata.common.AjaxResult;
import kn.pulldata.req.ActionReq;
import kn.pulldata.req.AddReq;
import kn.pulldata.req.UpdateReq;
import kn.pulldata.service.ExternalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author <a href="mailto:chenxilzx1@gmail.com">theonefx</a>
 */
@RestController
@Validated
@RequestMapping("/domain")
public class BasicController {



    @Autowired
    private ExternalService externalService;

    /**
     * 更新数据源状态
     * @param req
     * @return
     */
    @PostMapping("/action")
    @ResponseBody
    public AjaxResult action(@RequestBody @Valid ActionReq req) {
        return AjaxResult.success(externalService.action(req));
    }

    /**
     * 查询数据源列表
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    public AjaxResult list() {
        return AjaxResult.success(externalService.list());
    }
    /**
     * 新增数据源
     * @param req
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(@RequestBody @Valid AddReq req) {
        return AjaxResult.success(externalService.add(req));
    }

    /**
     * 修改数据源
     * @param req
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    public AjaxResult update(@RequestBody @Valid UpdateReq req) {
        return AjaxResult.success(externalService.update(req));
    }

    /**
     * 删除数据源
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}/{orgId}")
    @ResponseBody
    public AjaxResult delete(@PathVariable("id") Integer id,
                             @PathVariable("orgId") Integer orgId) {
        return AjaxResult.success(externalService.delete(id,orgId));
    }

    /**
     * 搜索监测方案
     * @param dataSoureId
     * @return
     */
    @GetMapping("/search/{dataSoureId}")
    @ResponseBody
    public AjaxResult search(@PathVariable("dataSoureId") Integer dataSoureId) {
        return AjaxResult.success(externalService.search(dataSoureId));
    }

}
